﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using WallpaperForm.Entities;
using WMPLib;
using static System.Windows.Forms.ListViewItem;

namespace WallpaperForm
{
    public partial class Group : Form
    {
        private List<WallpaperGroup> groups;
        private string _moveDir;
        private Unit.ShowLogDelegate _showLog;
        private ZyWallpaperContext _dbContext;

        public Group(string moveDir = null, Unit.ShowLogDelegate showLog = null)
        {
            _dbContext = new ZyWallpaperContext();
            this.groups = _dbContext.WallpaperGroups.OrderByDescending(x => x.GroupName).ToList();

            _moveDir = moveDir;
            _showLog = showLog;
            

            InitializeComponent();
        }

        private void Videos_Load(object sender, EventArgs e)
        {
            initListView();
        }

        /// <summary>
        /// 初始化视图
        /// </summary>
        private void initListView()
        {
            videoList.Columns.Add("类别名称", 200);
            videoList.Columns.Add("类别说明", 200);
            videoList.Columns.Add("数量", 200);
            videoList.Items.Clear();
            var itemList = new List<ListViewItem>();
            var fileCount = _dbContext.WallpaperItems.GroupBy(x => x.GroupName).Select(x => new
            {
                GroupName = x.Key,
                Count = x.Count(),
            }).ToList();
            groups.ForEach(group =>
            {
                var sub = new List<ListViewSubItem> {
                    new ListViewSubItem
                    {
                         Text=group.GroupName
                    },
                    new ListViewSubItem{Text=group.Description},
                    new ListViewSubItem{Text=$"{fileCount.FirstOrDefault(x=>x.GroupName==group.GroupName)?.Count??0}个" }
                };
                videoList.Items.Add(new ListViewItem(sub.ToArray(), groups.IndexOf(group)));
            });
        }
        private void videoList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
       

        /// <summary>
        /// 鼠标双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (videoList.SelectedItems.Count > 0)
            {
                var selectItemFirst = videoList.SelectedItems[0];
                var selectedObj = groups.FirstOrDefault(x => x.GroupName == selectItemFirst.Text);

                var video = new Videos(selectedObj.GroupName,_moveDir, _showLog);
                video.Show();
            }
        }

    }
}
